// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/functions/prestosql/types/json_registration.h>

#include <pollux/common/constrained_generators.h>
#include <pollux/functions/prestosql/types/json_cast_operator.h>
#include <pollux/functions/prestosql/types/json_type.h>
#include <pollux/type/type.h>

namespace kumo::pollux {
    namespace {
        class JsonTypeFactories : public CustomTypeFactories {
        public:
            JsonTypeFactories() = default;

            TypePtr getType(const std::vector<TypeParameter> &parameters) const override {
                POLLUX_CHECK(parameters.empty());
                return JSON();
            }

            exec::CastOperatorPtr getCastOperator() const override {
                return std::make_shared<JsonCastOperator>();
            }

            AbstractInputGeneratorPtr getInputGenerator(
                const InputGeneratorConfig &config) const override {
                static const std::vector<TypePtr> kScalarTypes{
                    BOOLEAN(),
                    TINYINT(),
                    SMALLINT(),
                    INTEGER(),
                    BIGINT(),
                    REAL(),
                    DOUBLE(),
                    VARCHAR(),
                };
                fuzzer::FuzzerGenerator rng(config.seed_);
                return std::make_shared<fuzzer::JsonInputGenerator>(
                    config.seed_,
                    JSON(),
                    config.nullRatio_,
                    fuzzer::getRandomInputGenerator(
                        config.seed_,
                        fuzzer::randType(rng, kScalarTypes, 3),
                        config.nullRatio_),
                    false);
            }
        };
    } // namespace

    void registerJsonType() {
        registerCustomType("json", std::make_unique<const JsonTypeFactories>());
    }
} // namespace kumo::pollux
